package EA.testproblems;
import EA.*;

/**
This testproblem is a simple problem for initial tuning of multimodal 
optimization algorithms. <br><br>

<table border="0" cellpadding="2" cellspacing="0">
<tr bgcolor="#a0a0a0">
  <td colspan="2" valign="top"><b>Problem description</b></td>
</tr>
<tr bgcolor="#e0e0e0">
  <td valign="top" width="200"><b>Name:</b></td>
  <td valign="top">Ursem multimodal 1</td>
</tr>
<tr bgcolor="#e0e0e0">
  <td valign="top"><b>Nickname:</b></td>
  <td valign="top">&nbsp;</td>
</tr>
<tr bgcolor="#e0e0e0">
  <td valign="top"><b>Intended usage:</b></td>
  <td valign="top">Initial finetuning and tests of especially multimodal algorithms.</td>
</tr>

<tr>
  <td colspan="2" valign="top">&nbsp;</td>
</tr>
<tr bgcolor="#a0a0a0">
  <td colspan="2" valign="top"><b>Problem details</b></td>
</tr>
<tr bgcolor="#e0e0e0">
  <td valign="top"><b>Function:</b></td>
  <td valign="top">sin(2x - 0.5pi) + 3cos(y) + 0.5x</td>
</tr>
<tr bgcolor="#e0e0e0">
  <td valign="top"><b>Plots:</b></td>
  <td valign="top"><img src="../../images/testproblems/ursemmultimodal1.gif">&nbsp;&nbsp;
<img src="../../images/testproblems/ursemmultimodal1_contour.gif"></td>
</tr>
<tr bgcolor="#e0e0e0">
  <td valign="top"><b>Ranges:</b></td>
  <td valign="top">x = [-2.5:3.0]&nbsp;&nbsp;y = [-2.0:2.0] </td>
</tr>
<tr bgcolor="#e0e0e0">
  <td valign="top"><b>Type:</b></td>
  <td valign="top">Maximization</td>
</tr>
<tr bgcolor="#e0e0e0">
  <td valign="top"><b>No. of maximas:</b></td>
  <td valign="top">2</td>
</tr>
<tr bgcolor="#e0e0e0">
  <td valign="top"><b>No. of minimas:</b></td>
  <td valign="top">6</td>
</tr>
<tr bgcolor="#e0e0e0">
  <td valign="top"><b>Optima radius:</b></td>
  <td valign="top">0.2</td>
</tr>
<tr bgcolor="#e0e0e0">
  <td valign="top"><b>Optima descriptions:</b></td>
  <td valign="top">The two maximas are located along the x-axis. They don't 
have the same height. The six minimas are located on the edge of the
searchspace.</td>
</tr>
<tr bgcolor="#e0e0e0">
  <td valign="top"><b>Known optimas:</b></td>
  <td valign="top">
  GMAX(1.697136454,0),
  LMAX(-1.444456199,0),
  LMIN(-0.1263401276,2), 
  LMIN(-0.1263401276,-2),
  LMIN(-2.5,2.0),
  LMIN(-2.5,-2.0),
  LMIN(3.0,2.0),
  LMIN(3.0,-2.0)
<br><font size=1>Capital letters 
means that the precise optima is known, lowercase letters is the best known 
so far.</font></td>
</tr>
<tr>
  <td colspan="2" valign="top">&nbsp;</td>
</tr>
<tr bgcolor="#a0a0a0">
  <td colspan="2" valign="top"><b>Plotting details</b></td>
</tr>

<tr bgcolor="#e0e0e0">
  <td valign="top"><b>GNUPlot code:</b></td>
  <td valign="top">
  set hidden3d<br>
  set isosamples 50<br>
  set view 80,15<br>
  splot [-2.5:3] [-2:2] sin(2*x-0.5*pi) + 3*cos(y) +0.5*x</td>
</tr>

</table>

y*log(x*k*k)/log(log(x*k*k))

splot [0:0.5] [0.0001:0.65] (1-y*log(x*2*2)/log(log(x*2*2)))**2 + (2-y*log(x*11*11)/log(log(x*11*11)))**2 + (3-y*log(x*113*113)/log(log(x*113*113)))**2 + (4-y*log(x*2089*2089)/log(log(x*2089*2089)))**2 + (5-y*log(x*28279*28279)/log(log(x*28279*28279)))**2

plot [y=0:0.7] (1-y*log(x*2*2)/log(log(x*2*2)))**2 + (2-y*log(x*11*11)/log(log(x*11*11)))**2 + (3-y*log(x*113*113)/log(log(x*113*113)))**2 + (4-y*log(x*2089*2089)/log(log(x*2089*2089)))**2 + (5-y*log(x*28279*28279)/log(log(x*28279*28279)))**2, x=4.362976015006986


Math.pow(1-func(realpos,2),2) + Math.pow(2-func(realpos,11),2) + Math.pow(3-func(realpos,113),2) +
		Math.pow(4-func(realpos,2089),2) + Math.pow(5-func(realpos,28279),2)
*/
public class RealMathematicalProblem1 extends NumericalProblem
{
  // Easier way to build max
  private double[][] lmax =  {};
  private double[][] lmin =  {};

  public RealMathematicalProblem1()
    {
      super();

      double[] optimas;

      name = "Real mathematical problem 1 (Jesper)";
      objectivefunction = new NumericalFitness(){
	private double func(double[] realpos, double k) {
	  return (realpos[1]*Math.log(realpos[0]*k*k))/Math.log(Math.log(realpos[0]*k*k));
	};

	      public double Fitness_calcFitness_inner(double[] realpos)
	      {
		return Math.pow(1-func(realpos,2),2) + Math.pow(2-func(realpos,11),2) + Math.pow(3-func(realpos,113),2) +
		Math.pow(4-func(realpos,2089),2) + Math.pow(5-func(realpos,28279),2);
	      };
	  };
      dimensions = 2;
      ismaximization = false;
      optimumradius = 0.2;

      intervals = new Interval[2];
      intervals[0] = new Interval(0.25000001,5);
      intervals[1] = new Interval(1E-15,0.7);

      // Set up known maximas
      knownmaxima = new NumericalOptimum[lmax.length];

      for (int i=0;i<lmax.length;i++) {
	optimas = new double[dimensions];
	optimas[0] = lmax[i][0];
	optimas[1] = lmax[i][1];
	knownmaxima[i] = new NumericalOptimum(optimas, objectivefunction.calcFitness(optimas), true, false, i);
      }

      // Set up known minimas
      knownminima = new NumericalOptimum[lmin.length];

      for (int i=0;i<lmin.length;i++) {
	optimas = new double[dimensions];
	optimas[0] = lmin[i][0];
	optimas[1] = lmin[i][1];
	knownminima[i] = new NumericalOptimum(optimas, objectivefunction.calcFitness(optimas), false, false, i);
      }
    }
}



